//
//  WmcRecognitionLoadingView.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/10/26.
//

import UIKit

class WmcRecognitionLoadingView: WmcBaseView {
    // Internal

    @discardableResult
    static func showInView(_ view: UIView) -> WmcRecognitionLoadingView {
        let subview = WmcRecognitionLoadingView()
        view.addSubview(subview)

        subview.setupUI()
        subview.makeConstraints()
        subview.startAnimation()
        subview.bindEvent()

        return subview
    }

    func dismiss() {
        removeFromSuperview()
    }

    // Private

    private lazy var contentView: UIView = {
        let view = UIView()
        view.backgroundColor = .wmc_white_FFFFFF
        view.layer.cornerRadius = 8
        view.layer.masksToBounds = true
        return view
    }()

    private lazy var grayView: UIView = {
        let view = UIView()
        view.backgroundColor = .wmc_gray_F7F8FA
        return view
    }()

    private lazy var layerView: UIView = {
        let view = UIView()
        view.frame = CGRect(x: -160, y: 0, width: 160, height: 74)

        let bgLayer = CAGradientLayer()
        bgLayer.colors = [UIColor(red: 0.32, green: 0.56, blue: 0.99, alpha: 0).cgColor, UIColor(red: 0.32, green: 0.56, blue: 0.99, alpha: 0.3).cgColor]
        bgLayer.locations = [0, 1]
        bgLayer.frame = view.bounds
        bgLayer.startPoint = CGPoint(x: 0, y: 0.5)
        bgLayer.endPoint = CGPoint(x: 0.5, y: 0.5)

        view.layer.addSublayer(bgLayer)
        return view
    }()

    private lazy var textLabel: UILabel = {
        let label = UILabel()
        label.text = "AI智能识别中"
        label.textColor = .wmc_grayH2_323233
        label.textAlignment = .center
        label.font = .systemFont(ofSize: 18, weight: .semibold)
        return label
    }()

    private lazy var closeBtn: UIButton = {
        let img = "close".wmc_image.yp.set(tintColor: UIColor.wmc_grayH8_8A8A99)
        let btn = UIButton()
        btn.setImage(img, for: .normal)
        btn.imageEdgeInsets = UIEdgeInsets(top: 1.5, left: 1.5, bottom: 1.5, right: 1.5)
        return btn
    }()

    private func setupUI() {
        backgroundColor = .black.withAlphaComponent(0.4)
        addSubview(contentView)
        contentView.addSubview(grayView)
        contentView.addSubview(layerView)
        contentView.addSubview(textLabel)
        contentView.addSubview(closeBtn)
    }

    private func makeConstraints() {
        snp.makeConstraints { make in
            make.left.top.right.bottom.equalToSuperview()
        }

        contentView.snp.makeConstraints { make in
            make.width.equalTo(320)
            make.height.equalTo(120)
            make.center.equalToSuperview()
        }

        grayView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(74)
        }

        closeBtn.snp.makeConstraints { make in
            make.width.height.equalTo(20)
            make.right.equalTo(-8)
            make.top.equalTo(8)
        }

        textLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.bottom.equalTo(-12)
        }
    }

    private func startAnimation() {
        UIView.animate(withDuration: 2.0, delay: 0.0, options: .curveEaseOut) {
            UIView.setAnimationRepeatCount(MAXFLOAT)
            self.layerView.frame = CGRect(x: 320, y: 0, width: 160, height: 74)
        } completion: { _ in
            self.layerView.frame = CGRect(x: -160, y: 0, width: 160, height: 74)
        }
    }

    private func bindEvent() {
        closeBtn.yp.addEvent { [weak self] _ in
            let alert = WmcAlertView()
            alert.title = "提示"
            alert.msg = "是否取消当前识别"
            alert.style = .custom(confirmTitle: "是", cancelTitle: "否")
            alert.confirmBlock = { [weak self] in
                guard let weakSelf = self else { return }
                weakSelf.dismiss()
                YPApp.CurrentVC?.navigationController?.popViewController(animated: true)
            }
            alert.show(height: 170)
        }
    }
}
